package com.seari.web.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.seari.bean.PlanShiftInfoBean;
import com.seari.bean.ReqPlanShiftBean;
import com.seari.pojo.MessageBean;
import com.seari.service.PlanSchedulingService;
import com.seari.service.PlanShiftInfoService;
import com.seari.web.vo.PlanShiftVO;

@Controller
public class PlanShiftController
{
	@Autowired
	PlanSchedulingService planSchedulingService;

	@Autowired
	PlanShiftInfoService planShiftInfoService;

	@RequestMapping(value = "plan/planShift")
	@ResponseBody
	public MessageBean getPlanShiftInfo(ReqPlanShiftBean bean)
	{
		MessageBean mb = new MessageBean();
		try
		{
			int exist = 0;
			exist = planShiftInfoService.checkPanShiftInfoExist(bean.getReqShiftDate(),bean.getReqShiftEndDate());
			if (exist > 0)
			{ // 如果存在
				List<Object> existShiftInfo = planShiftInfoService.fetchPlanShiftInfoList(bean);
				mb.setCode("success");
				mb.setData(existShiftInfo);
			} else
			{
				mb.setCode("info");
				mb.setMessage("当前无排班...");
			}
		} catch (Exception e)
		{
			mb.setCode("error");
			mb.setMessage("执行异常...");
		}
		return mb;
	}
	
	
	@RequestMapping(value = "plan/renderShift")
	@ResponseBody
	public MessageBean renderPlanShiftInfo(ReqPlanShiftBean bean) {
		MessageBean mb = new MessageBean();
		try
		{
			int exist = 0;
			exist = planShiftInfoService.checkPanShiftInfoExist(bean.getReqShiftDate(),bean.getReqShiftEndDate());
			if (exist > 0) {
				mb.setCode("warning");
				mb.setMessage("当前存在此班次...");
			}else {
				List<Object> planScheduInfo = planSchedulingService.fetchPlanSchedulingViewData(bean);
				mb.setCode("success");
				mb.setData(planScheduInfo);
			}
		} catch (Exception e)
		{
			mb.setCode("error");
			mb.setMessage("执行异常...");
		}
		return mb;
	}
	
	@RequestMapping(value = "plan/chgShift")
	@ResponseBody
	public MessageBean updatePlanShiftInfo(@RequestBody List<PlanShiftInfoBean> planShiftInfo) {
		MessageBean mb = new MessageBean();
		try
		{
			int count = planShiftInfoService.updatePlanShiftInfo(planShiftInfo);
			if(count > 0) {
				mb.setCode("success");
				mb.setMessage("调班任务执行成功!");
			}else {
				mb.setCode("error");
				mb.setMessage("调班任务执行失败!");
			}
		} catch (Exception e)
		{
			mb.setCode("error");
			mb.setMessage("调班任务执行异常...");
		}
		return mb;
	}
}
